# -*- coding: utf-8 -*-
from PyQt4 import QtCore , QtGui , uic
from lib.papyDB import papyDB , tableInfo
from lib import globalVar as GV
from lib.papyDBTable import *
from insertItem import insertItemDiag
from lib.itemModel import Asset
from lib.customUI import customUI
import os
import pickle
try :
    import maya.cmds as cmds
    import sys
    sys.path.append('C:\Python26\Lib\site-packages')
    sys.path.append('D:\kali\General\papyrus\py')
    import MySQLdb
    MAYA = 1
except :
    MAYA = 0



class shotPublisher( customUI ):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        uic.loadUi(r'ui/shotPublish.ui' , self)
        self.setUI()


    def setUI(self):
        self.publish_btn.clicked.connect(self.doPublish)     
        
        if GV.testUserID == '': self.userID = USER_INFO.search('userID' , userLogInName = os.environ['username'])
        else :                  self.userID = GV.testUserID
        self.userName = USER_INFO.search( 'userLogInName' , userID=self.userID )
        print self.userName
        self.user = USER_INFO.search( 'userName' , userID=self.userID )
        self.comID = USER_INFO.search('mID' , userID=self.userID )
        self.comName = USER_MACHINE.search('mName' , machineID=self.comID)
        self.userLevel = str( USER_INFO.search('userLevel' , userID=self.userID) )
        self.userText.setText(self.user)
        self.comText.setText(self.comName)
        self.levelText.setText(self.userLevel)
        
        prjList = PROJECT.search('prjCodeName')
        map(lambda x:self.prj_cb.addItem(x) , prjList ) 
        self.prj_cb.currentIndexChanged.connect( self.initSeqO )
        self.seq_cb.currentIndexChanged.connect( self.initShotO )
        self.show()
        self.prj_cb.setCurrentIndex(1)
        self.prj_cb.setCurrentIndex(0)
        

    def initSeqO(self):        
        self.initSeq('--Master--')   
        
             
    def initShotO(self):
        self.initShot('--Global--')     
        
        
    def doPublish(self):

        self.deptID = USER_INFO.search('deptID' , userID=self.userID )
        self.prjName = str( self.prj_cb.currentText() )
        self.seqName = str( self.seq_cb.currentText() )
        self.shotName = str( self.shot_cb.currentText() )   
        self.shotID = str ( SHOT.search("shotID" , shotName=self.shotName) )                 
        self.comment = str( self.cmmt_txt.toPlainText() )     
#        print self.deptID , self.prjName , self.seqName ,  self.shotName , self.comment
        
#        if self.deptID != "6" :
        self.jobInfoID = JOB_INFO.search("ID" , shotID=self.shotID , userID=self.userID)
        self.verCnt = papyDB.getLastID("JOB_VERSION", "jobVerID")
#            print self.jobInfoID , self.verCnt
#        elif self.deptID == "6" :
#            ######################### ?????????????????????????????????????????????????????? ##########################
#            self.jobInfoID = RENDER.search("ID" , shotID=self.shotID , userID=self.userID)
#            self.renPassID = RENDER_PASS.search("ID" , fID=self.jobInfoID)
#            self.verCnt = papyDB.getLastID("RENDER_PASS_VERSION", "ID")
##            print self.jobInfoID , self.renPassID , self.verCnt
   
   
        if self.jobInfoID != [] :
            self.jobInfoVer = JOB_VERSION.search("ver" , fID=self.jobInfoID)
            if self.jobInfoVer != [] :
                self.jobInfoVer = self.jobInfoVer[len(self.jobInfoVer)-1]+1 if type(self.jobInfoVer)==type([]) else self.jobInfoVer+1
            else : 
                self.jobInfoVer = 1
        ################################################################## self.deptID == "6" ??
#        print self.jobInfoID , self.jobInfoVer
            
     
        self.regShotVer()
        if len(self.comment) > 0 :
            self.regShotCmt()
        self.setStatus()
        self.saveShot()
        self.alertWin("Notice" , "Done!")
        
        
    def regShotVer(self):
        print self.jobInfoVer , self.jobInfoID
        papyDB.regTable('JOB_VERSION' , self.jobInfoVer , self.jobInfoID)
        
        
    def regShotCmt(self):        
        papyDB.regTable('JOB_CMMNT' , self.comment , self.verCnt+1)        
          
          
    def setStatus(self):        
        JOB_INFO.update('status' , 'WIP' , shotID=self.shotID , userID=self.userID)           
    
    
    def saveShot(self):
        s = os.sep
        case = 1
        if self.seqName == '--Master--' and self.shotName == '--Global--' :
            case = 0
        if self.shotName == '--Global--' and self.seqName != '--Master--' :
            self.shotName = '_library'
        deptInfo = []
        if self.deptID == 3     : deptInfo = ['Anim' , 'anim']
        elif self.deptID == 7   : deptInfo = ['FX' , 'FX']
        elif self.deptID == 10  : deptInfo = ['layout' , 'layout']
        elif self.deptID == 6   : deptInfo = ['Lighting' , 'light']
#        elif self.deptID == 8 : deptInfo = ['Render' , 'ren']
        ver = str(self.jobInfoVer).zfill(3)
        beforeVer = str(int(ver)-1).zfill(3)        
        
        newFile = self.shotName+'_'+deptInfo[1]+'_'+self.userName+'_'+ver+'.ma'
        oldPath = GV.tempMAYAfile
        if case == 0 :
            newPath = os.path.join( GV.PROJECT_SERVER , self.prjName , "VFX" , "_library" , deptInfo[0] , newFile )
        else :
            newPath = os.path.join( GV.PROJECT_SERVER , self.prjName , "VFX" , self.seqName , self.shotName , deptInfo[0] , newFile )


        if MAYA :
            cmds.file( GV.tempMAYAfile , o=True , f=True )
            if case == 1 :
                cmds.file( rn= GV.PROJECT_SERVER +s+s+ self.prjName +s+s+ "VFX" +s+s+ self.seqName +s+s+ self.shotName +s+s+ deptInfo[0] +s+s+ newFile )
            else :
                cmds.file( rn= GV.PROJECT_SERVER +s+s+ self.prjName +s+s+ "VFX" +s+s+ "_library" +s+s+ deptInfo[0] +s+s+ newFile )
            cmds.file( s=True , f=True )
        else : 
#            print newPath
            os.system("copy %s %s" % (oldPath , newPath) )


def theTool( ):
    if MAYA == True :
        global app    
        try:        
            app.close()        
        except :        
            app = shotPublisher()
        app.show()
    else :
        import sys
        app = QtGui.QApplication(sys.argv)
        mainWin = shotPublisher()   
        mainWin.show()
        sys.exit(app.exec_())

if __name__ == '__main__':
    theTool()            
        
#if __name__ == '__main__' :
#    import sys
#    app = QtGui.QApplication(sys.argv)
#    ui = shotPublisher()
##    ui.show()
#    app.exec_()
#    
#    